﻿

function UrlEncode(str){ 
   var ret=""; 
   var strSpecial="!\"#$%&'()*+,/:;<=>?[]^`{|}~%"; 
   for(var i=0;i<str.length;i++){ 
   var chr = str.charAt(i); 
     var c=str2asc(chr); 
     if(parseInt("0x"+c) > 0x7f){ 
       ret+="%"+c.slice(0,2)+"%"+c.slice(-2); 
     }else{ 
       if(chr==" ") 
         ret+="+"; 
       else if(strSpecial.indexOf(chr)!=-1) 
         ret+="%"+c.toString(16); 
       else 
         ret+=chr; 
     } 
   }  
   return ret; 
} 

function UrlDecode(str){ 
   var ret=""; 
   for(var i=0;i<str.length;i++){ 
   var chr = str.charAt(i); 
     if(chr == "+"){ 
       ret+=" "; 
     }else if(chr=="%"){ 
     var asc = str.substring(i+1,i+3); 
     if(parseInt("0x"+asc)>0x7f){ 
       ret+=asc2str(parseInt("0x"+asc+str.substring(i+4,i+6))); 
       i+=5; 
     }else{ 
       ret+=asc2str(parseInt("0x"+asc)); 
       i+=2; 
     } 
     }else{ 
       ret+= chr; 
     } 
   } 
   return ret; 
} 


function isEmpty(obj){
	  if(obj == null){
	  	  return true;
	  }
	  return false;
}

/*
======================================
   功能：全选或全部取消checkBox
   参数：checkBoxObj: 被全选或取消的checkBox控件对象
         functionBox: 发生事件的checkBox控件
         例：<input type="checkbox" name="all" onclick="checkAll(document.all.ids,this);">
   author: carroll
======================================
*/
function checkAll(checkBoxObj,functionBox){
	  if(functionBox.checked){
	  	  choiceAll(checkBoxObj);
	  }else{
	  	  deselect(checkBoxObj);
	  }
}

//设置checkbox全选
//author carroll
function choiceAll(checkBoxObj){
    		
    if(checkBoxObj != null){
    	  if(checkBoxObj.length == null){
    	  	  checkBoxObj.checked = true;
    	  }else{
    	  	  for(var i=0; i < checkBoxObj.length; i++){
		    	      checkBoxObj[i].checked = true;
		        }
    	  }
    	  
    }else{
    	  alert("No items can be choice!");
  	    return;
    }
}

//设置checkbox全部取消
//author carroll
function deselect(checkBoxObj){
	$(".allCheck").attr("checked",false);
	  if(checkBoxObj !=null){
	  	  if(checkBoxObj.length == null){
	  	  	  checkBoxObj.checked = false;
	  	  }else{
	  	  	  for(var i = 0; i < checkBoxObj.length; i++){
			  	      checkBoxObj[i].checked = false;
			      }
	  	  }
	  	  
	  }else{
	  	  alert('No items can be cancel!');
	  }
	  
} 

//判断checkbox控件是否有被选中的，如果有返回true,否则返回false
function isChecked(checkBoxObj){
	  if(checkBoxObj == null){
	  	  return false;
	  }else{
	  	  if(checkBoxObj.length == null && checkBoxObj.checked){
	  	  	return true
	  	  }
	  	  for(var i = 0; i < checkBoxObj.length; i++){
	  	  	if(checkBoxObj[i].checked){
	  	  		  return true;
	  	  	}
	  	  }
	  	  return false;
	  }
}


// 判断在多选的checkbox是否有选中，如果有则返回true，否则返回false
// auth kang
function checkboxvalidate(checkboxobj) {
    var isCheck=false;
	if(checkboxobj !=null ){
		if(checkboxobj.length==null && checkboxobj.checked) {
			isCheck = true;
		}else{
			for(i=0;i<checkboxobj.length;i++){
				if(checkboxobj[i].checked)
					isCheck = true;
			}
		}
	}
	return isCheck;
}

//
/*
======================================
   功能：用于按钮更新
   author: wangwei
======================================
*/
function goActionWithTargetDoCheckBox(index,action,target,checkboxobj){
	
		var myForm = document.forms[index];
		var checkCount = 0;
		if(checkboxobj != null){
				if(checkboxobj.length==null && checkboxobj.checked) {
							goActionWithTarget(index,action,target);
				}else{
						for(i=0;i<checkboxobj.length;i++){
								if(checkboxobj[i].checked)
										checkCount++;
						}
						goActionWithTarget(index,action,target);
						
				}
		}else{
				 alert('请选择需要操作的记录');
		}
}
function goActionWithTargetValidateCheckBox(index,action,target,checkboxobj){
	
		var myForm = document.forms[index];
		var checkCount = 0;
		if(checkboxobj != null){
				if(checkboxobj.length==null && checkboxobj.checked) {
							goActionWithTarget(index,action,target);
				}else{
						for(i=0;i<checkboxobj.length;i++){
								if(checkboxobj[i].checked)
										checkCount++;
						}
						if(checkCount >= 1){
								goActionWithTarget(index,action,target);
						}else{
								alert("请选择需要操作的记录");
								//deselect(checkboxobj);
						}
				}
		}else{
				 alert('请选择需要操作的记录');
		}
}
//验证启用部门时是否有作废状态的部门
function validateEnabledActive(ids,actives){
		if(checkboxvalidate(ids)){
				
				if(ids.length == null && ids.checked){
						if(actives.value == '1'){return false;}
				}else{
						for(i = 0 ; i < ids.length ; i++){
							if(ids[i].checked){
									if(actives[i].value == '1'){return false;}
							}
						}
				}
				return true;
		}
}

//验证作废部门时是否有启用状态的部门
function validateDisabledActive(ids,actives){
		if(checkboxvalidate(ids)){
				
				if(ids.length == null && ids.checked){
						if(actives.value == '2'){return false;}
				}else{
						for(i = 0 ; i < ids.length ; i++){
							if(ids[i].checked){
									if(actives[i].value == '2'){return false;}
							}
						}
				}
				return true;
		}
}
//作废 返回值 1为成功，2为失败
function goDeleteActionWithTargetByCustomIDs(index,action,target,ids,actives){
  var myForm = document.forms[index];
  var value = 2;
  if( checkboxvalidate(ids) ) {
  	if(validateDisabledActive(ids,actives)){
      if( confirm("您确定要作废所选的记录吗？") ) {
          if(myForm.current!=null) {
              myForm.current.value = 1;
          }
          goActionWithTarget(index,action,target);
          value = 1;
      }
    }else{
    	alert('只可以作废状态为“启用”的记录!');
    }
  }else{
      alert('没有可以作废的记录！');
  }
  return value;
}
//启用 返回值 1为成功，2为失败
function goReuseActionWithTargetByCustomIDs(index,action,target,ids,actives){
  var myForm = document.forms[index];
  var value = 2;
  if( checkboxvalidate(ids) ) {
  	if(validateEnabledActive(ids,actives)){
      if( confirm("您确定要启用所选的记录吗？") ) {
          if(myForm.current!=null) {
              myForm.current.value = 1;
          }
          goActionWithTarget(index,action,target);
          value = 1;
      }
    }else{
	  	alert('只可以启用状态为“作废”的记录!');
	  }
  }else{
      alert('没有可以启用的记录！');
  }
  return value;
}
//发布
function goIssueActionWithTargetByCustomIDs(index,action,target,ids,issues){
  var myForm = document.forms[index];
  if( checkboxvalidate(ids) ) {
  	if(validateEnabledActive(ids,issues)){
      if( confirm("您确定要发布所选的记录吗？") ) {
          if(myForm.current!=null) {
              myForm.current.value = 1;
          }
          goActionWithTarget(index,action,target);
      }
    }else{
	  	alert('只可以发布状态为“撤销发布”的记录!');
	  }
  }else{
      alert('没有可以发布的记录！');
  }
}
//撤销发布
function goUnissueActionWithTargetByCustomIDs(index,action,target,ids,issues){
  var myForm = document.forms[index];
  if( checkboxvalidate(ids) ) {
  	if(validateDisabledActive(ids,issues)){
      if( confirm("您确定要撤销所选的记录吗？") ) {
          if(myForm.current!=null) {
              myForm.current.value = 1;
          }
          goActionWithTarget(index,action,target);
      }
    }else{
    	alert('只可以撤销状态为“发布”的记录!');
    }
  }else{
      alert('没有可以撤销的记录！');
  }
}
// 检查用户输入的是否为数字
function checknum(str){

    if( str.length == 0 ) {
        // 允许为空
        return true;
    }
    var patten = /^\-{0,1}[0-9]{0,}\.{0,1}[0-9]{0,}$/;
    if( str.match(patten) ) {
        // 是数字
        return true;
    }
    return false;
}
//判断数值是否为小数
function decimals(str){
			var regu = /^[-]{0,1}[0-9]{1,}$/;
		    if( str.length == 0 ) {
		        return true;
		    }
		    if(regu.test(str)) {
		        return false;
		    }else{
		    return true;
		    }
}
function checkIsNull(str)
{
if(str==null)
return false;
if(str.length==0)
return false;

return true;
}
// 翻页脚本
function pageHandle(current) {
	$("input[name='current']").val(current);
    document.forms[0].target = "_self";
    document.forms[0].submit();
}

// 为跳转页面设置
// xum
function pageHandleGoTo(str) {
	var r = /^[0-9]*[1-9][0-9]*$/;//正整数  
	// 检验是否空
	var inputGoTo=null;
	var inputCurrent=null;
	var arrInputGoTo=$("input[name='goTo']");
	var num=null;
	var arrInputCurrent=$("input[name='current']");
	for(var i=0;i<arrInputGoTo.length;i++){
		if(arrInputGoTo[i].id==str){
			inputGoTo=arrInputGoTo[i];
			num=i;
			inputCurrent=arrInputCurrent[i];
		}
	}
	// 检验是否数字
	if(!r.test(inputGoTo.value)){
	  	alert("必须填写大于零的数字!");
	  	inputGoTo.value="";
		return;
 	}
	//  检验是否超过最大页数
	//modify by gongxinglin at 2011/11/9
	/**
	备注：如出现多个页签，最后一个验证有效。'commond有效时'，同个form表单中不能同时出现同名'commond'，否则'current'会成为数组
	*/
	if(inputGoTo.value > 2147483647){
		alert("页码超出数据范围!");
		return;
	}
	inputCurrent.value=inputGoTo.value;
	document.forms[num].submit();
}

function multipageHandle(current,index) {
	$("input[name='current']").val() = current;
    document.forms[index].submit();
}
// 提交用户表单 
function goActionWithTarget(index,action,target){
  var myForm = document.forms[index];
  myForm.action=action;
  if(target.length != 0) {
    myForm.target = target;
  }
  //为表单内每一个文本框值清除前后空格
   	for(var i=0;i<$(':text').length;i++)
   	{
    	$(':text:eq('+i+')').val($.trim($(':text:eq('+i+')').val()));
    
    }
  myForm.submit();
}

//数据验证
function goActionWithTargetDoValidation(formId,index,action,target) {
    if(doValidate(formId)){

	      goActionWithTarget(index,action,target);
	      return 1;
    }
    else{
    	  return 0;
    }
    
}

function goDeleteActionWithTarget(index,action,target){
  var myForm = document.forms[index];
  if( checkboxvalidate($("input[name='ids']")) ) {
      if( confirm("您确定要操作所选的项目吗？") ) {
          if(myForm.current!=null) {
              myForm.current.value = 1;
          }
          goActionWithTarget(index,action,target);
      }
  }else{
      alert('没有可以操作的项目！');
  }
}


function goDeleteActionWithTargetByIds(index,action,target,ids){
  var myForm = document.forms[index];
  if( checkboxvalidate(ids) ) {
      if( confirm("您确定要操作所选的项目吗？") ) {
          if(myForm.current!=null) {
              myForm.current.value = 1;
          }
          goActionWithTarget(index,action,target);
      }
  }else{
      alert('没有可以操作的项目！');
  }
}

//为弹开页设计，提交用户表单扩展，列表页不丢失action
function goActionWithTargetByOpen(index,action,target){
  var myForm = document.forms[index];
  var tempAction = myForm.action;
  myForm.action=action;
  if(target.length != 0) {
    myForm.target = target;
  }
  myForm.submit();
  myForm.action=tempAction;
}

//为弹开页设计，提交用户表单扩展，列表页不丢失action moddify by Huosd 2011-04-27
function goActionWithTargetValidateCheckBoxByOpen(index,action,target,checkboxobj){
	var myForm = document.forms[index];
	var checkCount = 0;
	if(checkboxobj != null){
		if(checkboxobj.length==null && checkboxobj.checked) {
			goActionWithTargetByOpen(index,action,target);
		}else{
			for(i=0;i<checkboxobj.length;i++){
				if(checkboxobj[i].checked){
					checkCount++;
				}
			}
			if(checkCount >= 1){
				goActionWithTargetByOpen(index,action,target);
			}else{
				alert("请选择需要操作的记录");
			}
		}
	}else{
		alert('请选择需要操作的记录');
	}
}

//清除查询条件，参数为要清除查询条件的域name名称，参数形式为"A,B,C,D"，中间以","分隔
function clearSearchValue(clearValue){
	var clearTmp = clearValue.split(",");
	for (var i=0;i<clearTmp.length;i++){
		if (clearTmp[i]!=undefined&&""!=clearTmp[i]){
			if ($("input[name='"+clearTmp[i]+"']").attr("type")=="radio"||$("input[name='"+clearTmp[i]+"']").attr("type")=="checkbox")
			{
				//radio标签或者checkbox标签
				$("input[name='"+clearTmp[i]+"']").attr("checked", false);
			}else {
				//text标签或者其他
				$("input[name='"+clearTmp[i]+"']").val("");
			}
		}
	}
}